package com.pactera.miyuangroup.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.pactera.miyuangroup.db.entity.RoleMenu;
import com.pactera.miyuangroup.db.mapper.RoleMenuMapper;
import com.pactera.miyuangroup.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单关系表 服务实现类
 * </p>
 *
 * @author WangWu
 * @since 2018-04-13
 */
@Service
public class RoleMenuServiceImp extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    /**
     * 绑定角色菜单
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    @Transactional
    public boolean BindRoleMenu(String roleId, List<String> menuIds) {

        EntityWrapper<RoleMenu> roleMenuEntityWrapper = new EntityWrapper<>();
        roleMenuEntityWrapper.eq("role_id", roleId);
        boolean flg = delete(roleMenuEntityWrapper);
        List<RoleMenu> roleMenus = new ArrayList<>();
        for (String m : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(m);
            roleMenu.setRoleId(roleId);
            roleMenus.add(roleMenu);
        }
        flg = insertBatch(roleMenus);
        return flg;
    }
}
